﻿Imports Models.Users

Namespace Security

    Public Class Companies

        Private companyList As Generic.List(Of FullCompanyVO)

        Sub New()

            companyList = New Generic.List(Of FullCompanyVO)

            '' Map GeoData Company
            Dim vo As New FullCompanyVO()

            vo.code = "PUBLIC"
            vo.name = "MapGeoData"
            'vo.licenseType = "FULL"
            'vo.contact = "NOVIX"

            Dim group As New GroupVO()
            group.name = "Default Group"
            group.code = "DEFAULT"
            group.system = False

            vo.groups.Add(group)
            companyList.Add(vo)


        End Sub


        ''' <summary>
        ''' Sincronize companies
        ''' </summary>
        ''' <remarks>Se agregara un grupo por default a las companies que existan</remarks>
        Public Sub sincronize()

            Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")


                '' add/update all companies
                Dim dbCompanyList As Generic.List(Of Data.BaseModel.Company) = (From c In oDB.Companies Select c).ToList
                Dim foundCompany As Boolean
                Dim foundGroup As Boolean
                For Each company As FullCompanyVO In companyList


                    foundCompany = False

                    '' locate company in db
                    For Each dbCompany As Data.BaseModel.Company In dbCompanyList
                        If dbCompany.Code = company.code Then
                            dbCompany.Name = company.name
                            dbCompany.System = True


                            For Each group As GroupVO In company.groups
                                foundGroup = False
                                For Each oGroup As Data.BaseModel.Group In dbCompany.Groups
                                    If group.code = oGroup.Code Then
                                        oGroup.Name = group.name
                                        foundGroup = True
                                    End If


                                Next
                                If Not foundGroup Then
                                    Dim oGroup As New Data.BaseModel.Group
                                    oGroup.System = group.system
                                    oGroup.Name = group.name
                                    oGroup.Code = group.code

                                    dbCompany.Groups.Add(oGroup)
                                End If
                            Next

                            foundCompany = True
                        End If
                    Next

                    '' create if not found
                    If Not foundCompany Then
                        Dim entity As New Data.BaseModel.Company()
                        entity.CompanyId = company.companyId
                        entity.Name = company.name
                        entity.Code = company.code
                        'entity.LicenseType = company.licenseType
                        'entity.Contact = company.contact
                        entity.System = True

                        Dim oGroup As Data.BaseModel.Group
                        For Each group As GroupVO In company.groups
                            oGroup = New Data.BaseModel.Group
                            oGroup.System = group.system
                            oGroup.Name = group.name
                            oGroup.Code = group.code

                            entity.Groups.Add(oGroup)
                        Next
                        oDB.Companies.AddObject(entity)
                    End If
                Next


                oDB.SaveChanges()

            End Using

        End Sub

    End Class

End Namespace